package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.repository.StrategyCommentRepository;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository strategyCommentRepository;

    @Autowired
    private MongoTemplate template;



    @Override
    public StrategyComment get(String id) {
        return strategyCommentRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> list() {
        return strategyCommentRepository.findAll();
    }

    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyCommentRepository.save(strategyComment);
    }


    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        // 查询条件
        Query query = new Query();

        if (StringUtils.hasLength(qo.getStrategyId())) {
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }

        // 拼接高级查询条件
        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").regex(".*"+qo.getKeyword()+".*"));
        }

        // 列表数据
        PageRequest request = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "_id");

        return DBHelper.query(template,StrategyComment.class,query,request);
    }

    @Override
    public void addComment(StrategyComment strategyComment) {
        strategyComment.setCreateTime(new Date());
        this.save(strategyComment);
    }

    @Override
    public void commentThumb(String cid, String uid) {
        // 1.查看当前操作是 点赞还是取消
        StrategyComment comment = this.get(cid);

        if (comment.getThumbuplist().contains(uid)){
            // 取消操作
            comment.setThumbupnum(comment.getThumbupnum() -1);  // 点赞数减一
            comment.getThumbuplist().remove(uid);               // 移除评论id集合中的当前用户id
        }else{
            // 点赞操作
            comment.setThumbupnum(comment.getThumbupnum() +1);  // 点赞数加一
            comment.getThumbuplist().add(uid);                  // 给评论id集合中添加当前用户id
        }
        strategyCommentRepository.save(comment);
    }


}
